﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Ewts.Storage
{
    public partial class TemporaryStorageList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

           
 
        /// <summary>
        /// 绑定临时库表
        /// </summary>
        protected void BindData()
        {
            DataTable dt = new DataTable();
            dt = new DataLib.BLL.InStorage().GetList("wrlno like 'M%' and status=1 order by id desc").Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                DataTable reptdt = GetDataByInStroageList(dt);
                rept.DataSource = reptdt;
                rept.DataBind();
            }

        }

        /// <summary>
        /// 根据入库单列表获取整个CJO列表信息
        /// </summary>
        /// <param name="dt_inList">称重列表</param>
        /// <returns></returns>
        protected DataTable GetDataByInStroageList(DataTable dt_inList)
        {
            

            DataTable dt = new DataTable();
            dt.Columns.Add("id");               //入库单id
            dt.Columns.Add("wrlno");            //入库编号
            dt.Columns.Add("loadnumno");        //Load号
            dt.Columns.Add("cjono");            //CJO号
            dt.Columns.Add("cjoid");
            dt.Columns.Add("customercode");     //客户编码
            dt.Columns.Add("companyname");      //客户名称
            dt.Columns.Add("instoragetime");    //入库时间
            dt.Columns.Add("loadnwt");          //Load单净重
            dt.Columns.Add("cjonwt");           //CJO单净重
            dt.Columns.Add("loadfinishnwt");    //Load已处理重量
            dt.Columns.Add("loadNofinishnwt");  //Load未处理重量
            dt.Columns.Add("isCategory");       //分类入库情况
            dt.Columns.Add("isLock");           //处理类型
            dt.Columns.Add("MakeStatus");       //原材料制作情况
         
            
            for (int i = 0; i < dt_inList.Rows.Count; i++)
            {
                DataRow dr = dt.NewRow();
                DataLib.Model.CJOItem cjomodel = GetCjoByInWrl(dt_inList.Rows[i]["id"].ToString());
                dr["id"] = dt_inList.Rows[i]["id"];
                dr["wrlno"] = dt_inList.Rows[i]["wrlno"];
                dr["loadnumno"] = cjomodel.loadnumno;
                dr["cjono"] = cjomodel.cjono;
                dr["customercode"] = cjomodel.customercode;
                dr["companyname"] = cjomodel.companyname;
                dr["cjoid"] = cjomodel.cjoid;
                dr["instoragetime"] = DateTime.Parse(dt_inList.Rows[i]["instoragetime"].ToString()).ToString("yyyy-MM-dd");
                dr["loadnwt"] = cjomodel.totalWeight;
                dr["loadfinishnwt"] = cjomodel.finishWeight;
                dr["loadNofinishnwt"] = cjomodel.totalWeight - cjomodel.finishWeight;
                dr["cjonwt"] = cjomodel.cjonwt;
                string strislock = "正常";
                if(dt_inList.Rows[i]["islock"] != null && dt_inList.Rows[i]["islock"].ToString()!="")
                {
                    bool bislock = false;
                    bislock = (bool)dt_inList.Rows[i]["islock"];
                    if (bislock == true)
                        strislock = "锁定";

                }
                dr["isLock"] = strislock;
                if (cjomodel.finishWeight == 0)
                {
                    dr["isCategory"] = "未处理";
                }
                else if (cjomodel.finishWeight == cjomodel.totalWeight)
                {
                    dr["isCategory"] = "已处理";
                }
                else
                {
                    dr["isCategory"] = "处理中";
                }

                DataTable dtTally = new DataLib.BLL.Tally().GetList("LoadNo='" + cjomodel.loadnumno + "'").Tables[0];
                if (dtTally != null && dtTally.Rows.Count > 0 && (dtTally.Rows[0]["status"].ToString() == "1"))
                    dr["MakeStatus"] = "已经做原材料";
                else
                    dr["MakeStatus"] = "未做原材料";
                dt.Rows.Add(dr);
            }


            return dt;
        }

        /// <summary>
        /// 根据入库ID获取整个CJO的明细对象
        /// </summary>
        /// <param name="inwrlid"></param>
        /// <returns></returns>
        protected DataLib.Model.CJOItem GetCjoByInWrl(string inwrlid)
        {
            DataLib.Model.CJOItem cjomodel = new DataLib.Model.CJOItem();
            string cjoid = "0";
            DataTable dt = new DataLib.BLL.BarCode().GetList("InStrorageID=" + inwrlid).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                cjoid = dt.Rows[0]["cjoid"].ToString().Trim();
            }
            cjomodel = new DataLib.BLL.CJOItem().GetListByCid(int.Parse(cjoid));
            return cjomodel;
        }
    }
}